import { getPlayList } from "../../services/index"
import musicStore from "../../stores/musicStore"
import menuStore from "../../stores/menuStore"

// pages/detail-music/detail-music.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    key:'',
    type:'recommend'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {type, id,key} = options
    this.setData({
      type:type
    })
    if (type === "ranking") {
      // const key = options.key
      this.data.key = key
      console.log(this.data.key)
      menuStore.onState(key, this.handleRanking)
    } else if (type === "menu") {
      this.data.id = id
      this.fetchMenuSongInfo()
    } else {
      musicStore.onState("recommendMusicList",this.handleRanking)
    }    
  },
  async fetchMenuSongInfo(){
    const res = await getPlayList(this.data.id)
    this.setData({ musicInfo: res.playlist })
  },
  handleRanking(value){
      this.setData({
        musicInfo:value
      })
      wx.setNavigationBarTitle({
        title: value.name,
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.type === "ranking") {
      console.log(this.data.key)
      musicStore.offState(this.data.key, this.handleRanking)
    } else if (this.data.type === "recommend") {
      musicStore.offState("recommendMusicList", this.handleRanking)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    
  }
})